package com.capinfo.config;

import com.capinfo.entity.SysDict;
import com.capinfo.service.DictService;
import com.capinfo.utils.RedisUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: sunbenjin
 * @date: 2020/3/1
 **/
@Component
public class WebStartUpRunner implements CommandLineRunner {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private DictService dictService;

    @Override
    public void run(String... args) throws Exception {
        SysDict dict = new SysDict();
        List<SysDict> dictList = dictService.selectListByPage(dict);
        //List<SysDict> dictList = dictService.selectAll();
        if(!CollectionUtils.isEmpty(dictList)){
            Map<String,List<SysDict>> groupByDictType = dictList.stream().collect(Collectors.groupingBy(SysDict::getDictType));
           //
            for(Map.Entry<String,List<SysDict>> entryDict:groupByDictType.entrySet()){
                String key = entryDict.getKey();
//                List<SysDict> lists = (List<SysDict>)

                List<Object> lists = redisUtils.lRange(key, 0, -1);
               // List<SysDict> lists = (List<SysDict>) objects;
                if(CollectionUtils.isEmpty(lists)){
                    redisUtils.lPush(key,entryDict.getValue());
                }
            }

            dictList.stream().forEach((sysDict)->{redisUtils.set(sysDict.getDictType()+sysDict.getDictValue(),sysDict);});
//            dictList.stream().forEach((sysDict)->{redisUtils.set(sysDict.getDictType()+sysDict.getDictValue(),sysDict.getDictName());});
        }

    }
}
